 package com.baizhi.zjs.action;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.baizhi.zjs.util.ImageCodeUtil;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class Ce1Action extends ActionSupport{
	//验证码
	public String codea() throws Exception{
		System.out.println("========");
		ImageCodeUtil util = new ImageCodeUtil();
		//获得随机字符
		String securityCode =util. getSecurityCode();
		//打印随机字符
		System.out.println("===="+securityCode);
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.setAttribute("securityCode", securityCode);
		//生成图片
		BufferedImage image = util.createImage(securityCode);
		//将生成的验证码图片以png(1.png)的格式输出到D盘        "D:\\1.png"   ==  "D:/1.png"
		//ImageIO.write(image, "png", new FileOutputStream(new File("D:\\4.png")));
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("image/png");
		ServletOutputStream outputStream = response.getOutputStream();
		ImageIO.write(image, "png",  response.getOutputStream());
		//outputStream.close();
		return Action.NONE;
	}
}
